#define _CRT_SECURE_NO_WARNINGS 1

bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize)
{
    int* stack = (int*)malloc(pushedSize * sizeof(int));
    if (NULL == stack)
    {
        printf("malloc fail\n");
        exit(-1);
    }

    int i = 0, j = 0;
    int top = 0;
    for (i = 0; i < pushedSize; ++i)
    {
        stack[top] = pushed[i];
        ++top;

        while (top > 0 && stack[top - 1] == popped[j])
        {
            ++j;
            --top;
        }
    }

    free(stack);

    return 0 == top;
}